import { defineConfig } from "vite";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const currentDir = path.dirname(__filename);
function getDirFiles(dir) {
    let files = fs.readdirSync(dir, { withFileTypes: true });
    let arr = [];
    for (let i = 0; i < files.length; i++) {
        let item = files[i];
        if (item.isFile() && path.extname(item.name) === ".ts") {
            let filePath = path.resolve(dir, item.name);
            arr.push(filePath);
        }
    }
    return arr;
}
function getAllEntry() {
    let allEntry = ["./src/index.ts"];
    let controller = path.resolve(currentDir, "src", "controller");
    let service = path.resolve(currentDir, "src", "service");
    allEntry.push(...getDirFiles(controller), ...getDirFiles(service));
    return allEntry;
}

export default defineConfig({
    build: {
        minify: false,
        emptyOutDir: false,
        lib: {
            entry: getAllEntry(),
            name: "babyGo",
            fileName: function (formatter, entryName) {
                return `${entryName}.js`;
            },
            formats: ["cjs"],
        },
        rollupOptions: {
            external: ["utils", "request", "crypto", "uni-cloud-router"],
            output: {
                dir: "./",
                preserveModules: true,
            },
        },
    },
});
